﻿package kliment.display {
	import flash.display.MovieClip;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author	Kliment
	 * @version	1.0.0
	 * 
	 * class name: kliment.display.MovieClip9Slice
	 */
	public class MovieClip9Slice extends MovieClip {
		private var _width:Number;
		private var _height:Number;
		
		private var _min_width:Number = 25;
		private var _min_height:Number = 25;
		
		private var _tl_mc:MovieClip;
		private var _tr_mc:MovieClip;
		private var _bl_mc:MovieClip;
		private var _br_mc:MovieClip;
		
		private var _ct_mc:MovieClip;
		private var _cl_mc:MovieClip;
		private var _cb_mc:MovieClip;
		private var _cr_mc:MovieClip;
		private var _c_mc:MovieClip;
	
		public function MovieClip9Slice() {
			_tl_mc = this['tl_mc'];
			_tr_mc = this['tr_mc'];
			_bl_mc = this['bl_mc'];
			_br_mc = this['br_mc'];
			_ct_mc = this['ct_mc'];
			_cl_mc = this['cl_mc'];
			_cb_mc = this['cb_mc'];
			_cr_mc = this['cr_mc'];
			_c_mc = this['c_mc'];
			
			_ct_mc.y = _tr_mc.y = _tl_mc.y;
			_cl_mc.x = _bl_mc.x = _tl_mc.x;
		}
		
		override public function get width():Number { return _width }
		override public function set width(value:Number):void {
			value = Math.max(value, _min_width);
			_width = value;
			
			_setHorisontal();
		}
		
		override public function get height():Number { return _height }
		override public function set height(value:Number):void {
			value = Math.max(value, _min_height);
			_height = value;
			
			_setVaertical();
		}
		
		public function get min_width():Number { return _min_width; }
		public function set min_width(value:Number):void {
			_min_width = value;
		}
		
		public function get min_height():Number { return _min_height; }
		public function set min_height(value:Number):void {
			_min_height = value;
		}
		
		private function _setHorisontal():void {
			_tr_mc.x = 
			_br_mc.x = _width + _tl_mc.x;
			
			var tlBounds:Rectangle = _tl_mc.getBounds(_tl_mc.parent);
			var blBounds:Rectangle = _bl_mc.getBounds(_bl_mc.parent);
			
			var trBounds:Rectangle = _tr_mc.getBounds(_tr_mc.parent);
			var brBounds:Rectangle = _br_mc.getBounds(_br_mc.parent);
			
			_cr_mc.x = _tr_mc.x;
			
			_ct_mc.x = tlBounds.right;
			_ct_mc.width = trBounds.x - tlBounds.right;
			
			_cb_mc.x = blBounds.right;
			_cb_mc.width = brBounds.x - blBounds.right;
			
			_c_mc.x = _ct_mc.x;
			_c_mc.width = _ct_mc.width;
		}
		
		private function _setVaertical():void {
			_bl_mc.y = 
			_br_mc.y = _height + _tl_mc.y;
			
			var tlBounds:Rectangle = _tl_mc.getBounds(_tl_mc.parent);
			var blBounds:Rectangle = _bl_mc.getBounds(_bl_mc.parent);
			
			var trBounds:Rectangle = _tr_mc.getBounds(_tr_mc.parent);
			var brBounds:Rectangle = _br_mc.getBounds(_br_mc.parent);			
			
			_cb_mc.y = _bl_mc.y;
			
			_cl_mc.y = tlBounds.bottom;
			_cl_mc.height = brBounds.y - tlBounds.bottom;
			
			_cr_mc.y = trBounds.bottom;
			_cr_mc.height = brBounds.y - trBounds.bottom;
			
			_c_mc.y = _cl_mc.y;
			_c_mc.height = _cl_mc.height;
		}
	}

}